//
//  SHD_SearchDataCollectionCell.m
//  B段项目
//
//  Created by dllo on 15/11/10.
//  Copyright © 2015年 谢美娜. All rights reserved.
//

#import "SHD_SearchDataCollectionCell.h"
#import "GZ_PlayerPageVC.h"
#import "SHD_UserDataViewController.h"
@implementation SHD_SearchDataCollectionCell
-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
//        [self getData];
        [self getTabeleVeiw];
    }
    return self;
}
#pragma mark tableView
-(void)getTabeleVeiw
{
    //初始化数组

    _myTableView = [[UITableView alloc]init];
    _myTableView.delegate = self;
    _myTableView.dataSource = self;
    [_myTableView NightWithType:UIViewColorType2];
    [self.contentView addSubview:_myTableView];
    
    
}
-(void)layoutSubviews
{
    [super subviews];
    _myTableView.frame = self.contentView.frame;
}
#pragma mark tableView 方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    

    switch (section) {
        case 0:
            return _userArray.count;
            break;
            case 1 :
                       return _tingArray.count;
            
        case 2:
                      return  _contectArray.count;
            case 3:
                        return _topicArray.count;
            
        default:
            break;
    }
    return 3;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *userIden = @"userCell";
    SHD_SearUserTableCell *userCell = [tableView dequeueReusableCellWithIdentifier:userIden];
    if (!userCell) {
        userCell = [[SHD_SearUserTableCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:userIden];
    }
    static NSString *tingIden = @"tingCell";
    SHD_SearTingTabelCell *tingCell = [tableView dequeueReusableCellWithIdentifier:tingIden];
    if (!tingCell) {
        tingCell = [[SHD_SearTingTabelCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tingIden];
    }

    static NSString *contectIden = @"contectCell";
    SHD_SearContectTableCell *contectCell = [tableView dequeueReusableCellWithIdentifier:contectIden];
    if (!contectCell) {
        contectCell = [[SHD_SearContectTableCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:contectIden];
    }

    static NSString *topicIden = @"topicCell";
    SHD_SearTopicTableCell *topicCell = [tableView dequeueReusableCellWithIdentifier:topicIden];
    if (!topicCell) {
        topicCell = [[SHD_SearTopicTableCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:topicIden];
    }
    
    switch (indexPath.section) {
        case 0:
            
            if (_userArray.count == 0) {
                userCell.textLabel.text = @"没有你想要的无结果";
                return userCell;
            }
                userCell.list = _userArray[indexPath.row];
            
            return userCell;
            break;
            case 1:
            if (_tingArray.count == 0) {
                 tingCell.titleLabel.text = @"没有你想要的无结果";
                return tingCell;
            }
            tingCell.list = _tingArray[indexPath.row];
            return tingCell;
            case 2:
            if (_contectArray.count == 0) {
                 contectCell.titleLabel.text = @"没有你想要的无结果";
                return contectCell;
            }
            contectCell.list = _contectArray[indexPath.row];
            return contectCell;
            break;
            case 3:
            if (_topicArray.count == 0) {
                 topicCell.titleLabel.text = @"没有你想要的无结果";
                return topicCell;
            }
            topicCell.list = _topicArray[indexPath.row];
            
            return topicCell;
           }
    return contectCell;
    
    
}
#pragma mark 点击进入详情
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    XMN_ReadDetailViewController *xrv = [[XMN_ReadDetailViewController alloc]init];
    XMN_PostsViewController *xpv = [[XMN_PostsViewController alloc]init];
    SHD_UserDataViewController *suv = [[SHD_UserDataViewController alloc]init];
       switch (indexPath.section) {
        case 0:
               suv.ID = [_userArray[indexPath.row] uid];
               [self.nav pushViewController:suv animated:YES];
            
            break;
            case 1:
               if (_tingArray.count == 0) {
                   
               }
           {
            [self.jumpDelegate jumpVC:[_tingArray[indexPath.row] tingid] ];
           }
               case 2:
               if (_contectArray.count == 0) {
                   
               }
               else
               {
               xrv.idString = [_contectArray[indexPath.row] contentid];
               [self.nav pushViewController:xrv animated:YES];
               }
               break;
               case 3:
               if (_topicArray.count == 0) {
                   
               }
               else
               {
               xpv.idString = [_topicArray[indexPath.row] contentid];
               xpv.contentString = [_topicArray[indexPath.row] shortcontent];
               xpv.titleString = [_topicArray[indexPath.row] title];
               [self.nav pushViewController:xpv animated:YES];
               }
               
        default:
            break;
    }
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 375 * FitWidth, 30 * FitHeight)];
    view.backgroundColor = [UIColor lightGrayColor];
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(15 * FitWidth, 5 * FitHeight, 200 * FitWidth, 20 * FitHeight)];
    label.text = _array[section];
    [view addSubview:label];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(275 * FitWidth, 5 * FitHeight, 100 * FitWidth, 20 * FitWidth);
    button.tag = 1000 + section;
    [button setTitle:@"更多结果" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [view addSubview:button];
    return view;
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return _array[section];
}
-(void)buttonAction:(UIButton *)button
{
    [self.delegate delegateMethond:button.tag- 999];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 4;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0 || indexPath.section == 1) {
        return 70 * FitHeight;
    }
    else{
        return 165 * FitHeight;
    }
}
#pragma mark 数据解析
- (void)setID:(NSString *)ID
{
    _ID = ID;
    
    [self getData];
}
-(void)getData
{
    [HTTPMethod getDataByString:@"http://api2.pianke.me/search/index" BODYDic:[ChangeString changeStringToDictionary:[NSString stringWithFormat:@"auth=&client=1&deviceid=5AA12B08-7AD2-4ED0-B4EB-571025443952&keyword=%@&version=3.0.6", _ID]] WithDataBlock:^(id data) {
        NSArray *array = [[data objectForKey:@"data"] objectForKey:@"alldetail"];
      _userArray = [NSMutableArray array];
        _tingArray = [NSMutableArray array];
        _contectArray = [NSMutableArray array];
        _topicArray = [NSMutableArray array];
        _dataDic = [NSMutableDictionary dictionary];
        _dataArray = [NSMutableArray array];
        for (NSDictionary *dic in array) {
            SHD_SearList *list = [[SHD_SearList alloc]init];
            [list setValuesForKeysWithDictionary:dic];
            [_dataArray addObject:list];
            
            
        }
        for (NSDictionary *dic in [array[0] objectForKey:@"list"])
         {
            SHD_UserList *list = [[SHD_UserList alloc]init];
            [list setValuesForKeysWithDictionary:dic];
            [_userArray addObject:list];
            
         }
        for (NSDictionary *dic in [array[1] objectForKey:@"list"]) {
            SHD_SearchTingList *list = [[SHD_SearchTingList alloc]init];
            [list setValuesForKeysWithDictionary:dic];
            [_tingArray addObject:list];
            
        }
            for (NSDictionary *dic in [array[2] objectForKey:@"list"]) {
            SHD_SearchContectList *list = [[SHD_SearchContectList alloc]init];
            [list setValuesForKeysWithDictionary:dic];
            [_contectArray addObject:list];
            
        }
        
        for (NSDictionary *dic in [array[3] objectForKey:@"list"]) {
            SHD_SearchTopicList *list = [[SHD_SearchTopicList alloc]init];
            [list setValuesForKeysWithDictionary:dic];
            [_topicArray addObject:list];
            
        }
        _array = [NSMutableArray arrayWithObjects:[NSString stringWithFormat:@"相关用户(%@)", [array[0] objectForKey:@"total"]], [NSString stringWithFormat:@"相关Ting(%@)", [array[1] objectForKey:@"total"]], [NSString stringWithFormat:@"相关故事(%@)", [array[2] objectForKey:@"total"]], [NSString stringWithFormat:@"相关huat(%@)", [array[3] objectForKey:@"total"]], nil];
        
        [_myTableView reloadData];
    }];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
